package cn.tedu.adminsystem.webapi.service.impl;

import cn.tedu.adminsystem.service.NoticeService;
import cn.tedu.adminsystem.service.PhotoUploadService;
import cn.tedu.adminsystem.webapi.mapper.NoticeMapper;
import cn.tedu.adminsystem.webapi.security.CurrentAdmin;
import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.pojo.notice.dto.NoticeAddNewDTO;
import cn.tedu.community.commons.pojo.notice.dto.NoticeUpdateDTO;
import cn.tedu.community.commons.pojo.notice.entity.Notice;
import cn.tedu.community.commons.pojo.notice.vo.AdmNoticeListItemVO;
import cn.tedu.community.commons.pojo.notice.vo.AdmNoticeStandardVO;
import cn.tedu.community.commons.pojo.notice.vo.UserNoticeListItemVO;
import cn.tedu.community.commons.pojo.notice.vo.UserNoticeStandardVO;
import cn.tedu.community.commons.restful.JsonPage;
import cn.tedu.community.commons.restful.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.service.GenericException;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 公告Service实现类
 */
@DubboService
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper mapper;

    @Autowired
    private PhotoUploadService photoService;
    //redis
//    @Autowired
//    private INoticeRedisRepository redisRepository;

    /**
     * 重建redis缓存
     */
   /* @Override
    public void rebuildCache() {
        log.debug("开始处理重建公告Redis缓存！无参数");
        redisRepository.deleteALl();

        List<AdmNoticeListItemVO> admNoticeList = mapper.getAdmNoticeList();
        List<UserNoticeListItemVO> userNoticeList = mapper.getUserNoticeList();
        redisRepository.saveAdmNoticeList(admNoticeList);
        redisRepository.saveUserNoticeList(userNoticeList);

        for (AdmNoticeListItemVO listItemVO : admNoticeList) {
            Long noticeId = listItemVO.getId();
            AdmNoticeStandardVO noticeStandardById = mapper.getAdmNoticeStandardById(noticeId);
            redisRepository.saveOne(noticeStandardById);
        }
        for (UserNoticeListItemVO listItemVO : userNoticeList) {
            Long noticeId = listItemVO.getId();
            UserNoticeStandardVO userNoticeStandardVO = mapper.getUserNoticeStandardById(noticeId);
            redisRepository.save(userNoticeStandardVO);
        }
    }*/

    /**
     * 添加公告
     *
     * @param newDTO 添加公告DTO
     */
    @Override
    public void addNew(NoticeAddNewDTO newDTO) {
        int count = mapper.countTitle(newDTO.getTitle());
        if (count > 0) {
            String message = "添加公告失败,标题已存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        String username = CurrentAdmin.getAdminInfo().getUsername();
        Notice notice = new Notice();
        BeanUtils.copyProperties(newDTO, notice);
        notice.setAdmName(username);
        int insertRows = mapper.insert(notice);
        if (insertRows != 1) {
            String message = "添加公告失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    /**
     * 删除公告
     *
     * @param id 公告id
     */
    @Override
    public void deleteById(Long id) {
        AdmNoticeStandardVO standardById = mapper.getAdmNoticeStandardById(id);
        if (standardById == null) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, "该公告已不存在！");
        }
        int deleteRows = mapper.deleteById(id);
        if (deleteRows != 1) {
            String message = "删除公告失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        if (standardById.getPhoto() != null) {
            photoService.deletePhoto(standardById.getPhoto());
        }
    }

    /**
     * 批量删除公告
     */
    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            AdmNoticeStandardVO standardById = mapper.getAdmNoticeStandardById(id);
            if (standardById == null) {
                String message = "抱歉，有某些(个)公告已不存在，请刷新页面后重新选择删除！";
                throw new ServiceException(ServiceCode.ERR_NOTFOUND, message);
            }
            if (standardById.getPhoto() != null) {
                photoService.deletePhoto(standardById.getPhoto());
            }
        }
        int deleteRows = mapper.deleteByIds(ids);
        if (deleteRows < ids.length) {
            String message = "删除某些(个)公告失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    /**
     * 修改公告
     *
     * @param updateDTO 修改公告DTO
     */
    @Override
    public void update(Long id, NoticeUpdateDTO updateDTO) {
        AdmNoticeStandardVO standardById = mapper.getAdmNoticeStandardById(id);
        if (standardById == null) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, "更新失败！该公告已不存在！");
        }
        Notice notice = new Notice();
        notice.setId(id);
        BeanUtils.copyProperties(updateDTO, notice);
        int updateRows = mapper.update(notice);
        if (updateRows != 1) {
            String message = "更新公告失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        if (standardById.getPhoto() != null && !updateDTO.getPhoto().equals(standardById.getPhoto())) {
            photoService.deletePhoto(standardById.getPhoto());
        }
    }

    /**
     * 查询详情到管理员页面
     *
     * @param id 公告id
     */
    @Override
    public AdmNoticeStandardVO getAdmNoticeStandardById(Long id) {
        AdmNoticeStandardVO standardVO = mapper.getAdmNoticeStandardById(id);
        if (standardVO == null) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, "抱歉，该公告已不存在！");
        }
        return standardVO;
    }

    /**
     * 查询详情到用户页面
     *
     * @param id 公告id
     */
    @Override
    public UserNoticeStandardVO getUserNoticeStandardById(Long id){
        UserNoticeStandardVO standardVO = mapper.getUserNoticeStandardById(id);
        if (standardVO == null) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, "抱歉，该公告已不存在!");
        }
        return standardVO;
    }

    /**
     * 查询列表到管理员页面
     *
     * @return AdmNoticeListItemVO集合
     */
    @Override
    public JsonPage<AdmNoticeListItemVO> getAdmNoticeList(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<AdmNoticeListItemVO> admNoticeListItemVOS = mapper.getAdmNoticeList();
        return JsonPage.restPage(new PageInfo<>(admNoticeListItemVOS));
    }

    /**
     * 模糊查询列表到管理员页面
     *
     * @param title 标题
     * @return AdmNoticeListItemVO集合
     */
    @Override
    public JsonPage<AdmNoticeListItemVO> getAdmNoticeListByTitle(String title, Integer page, Integer pageSize) {
        if (mapper.countByTitle(title) == 0) {
            throw new ServiceException(ServiceCode.ERR_NOTFOUND, "标题不存在！请重新输入!");
        }
        PageHelper.startPage(page, pageSize);
        List<AdmNoticeListItemVO> admNoticeListByTitle = mapper.getAdmNoticeListByTitle(title);
        return JsonPage.restPage(new PageInfo<>(admNoticeListByTitle));
    }

    /**
     * 查询列表到用户页面
     *
     * @return UserNoticeListItemVO集合
     */
    @Override
    public JsonPage<UserNoticeListItemVO> getUserNoticeList(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<UserNoticeListItemVO> userNoticeListItemVOS = mapper.getUserNoticeList();
        return JsonPage.restPage(new PageInfo<>(userNoticeListItemVOS));
    }

}
